package Dao.Admin.Imp;

import Dao.Admin.AccountUpdateDao;
import Entity.Role.Student;
import Entity.Role.Teacher;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AccountUpdateDaoImp implements AccountUpdateDao {
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    /**新增学生信息*/
    @Override
    public Integer studentAdd(Student student,Connection conn){
        int result = 0;
        try {
            String sql = " insert into stu_info( sno,password,name,sex,tel,email,collname,classes,role,major_name) " +
                    " values (?,?,?,?,?,?,?,?,'学生',?) ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,student.getSNO());
            ps.setString(2,student.getPassword());
            ps.setString(3,student.getName());
            ps.setString(4,student.getSex());
            ps.setString(5,student.getTel());
            ps.setString(6,student.getEmail());
            ps.setString(7,student.getCollName());
            ps.setString(8,student.getMajorName());
            ps.setString(9,student.getClasses());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }
    /**编辑学生信息*/
    @Override
    public Integer studentUpdate(Student student,String sno, Connection conn){
        int result = 0;
        try {
            String sql = " update stu_info set sno = ?,password = ?,name = ?,sex = ?,tel = ?,email = ?,collname = ?,classes = ?,major_name = ? " +
                    " where sno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,student.getSNO());
            ps.setString(2,student.getPassword());
            ps.setString(3,student.getName());
            ps.setString(4,student.getSex());
            ps.setString(5,student.getTel());
            ps.setString(6,student.getEmail());
            ps.setString(7,student.getCollName());
            ps.setString(8,student.getMajorName());
            ps.setString(9,student.getClasses());
            ps.setString(10,student.getSNO());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }
    /**删除学生信息*/
    @Override
    public Integer studentDelete(String sno, Connection conn){
        int result = 0;
        try {
            String sql = " delete from stu_Info where sno = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,sno);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }

    /**新增教师信息*/
    @Override
    public Integer teacherAdd(Teacher teacher,Connection conn){
        int result = 0;
        try {
            String sql = " insert into teacher( tno,password,name,sex,tel,email,collegeid,post,role,department) " +
                    " values (?,?,?,?,?,?,?,?,'教师',?) ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,teacher.getTNO());
            ps.setString(2,teacher.getPassword());
            ps.setString(3,teacher.getName());
            ps.setString(4,teacher.getSex());
            ps.setString(5,teacher.getTel());
            ps.setString(6,teacher.getEmail());
            ps.setString(7,teacher.getCollegeId());
            ps.setString(8,teacher.getPost());
            ps.setString(9,teacher.getDepartment());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }
    /**编辑教师信息*/
    @Override
    public Integer teacherUpdate(Teacher teacher, String tno, Connection conn){
        int result = 0;
        try {
            String sql = " update teacher set tno = ?,password = ?,name = ?,sex = ?,tel = ?,email = ?,collegeid = ?,post = ?,department = ? " +
                    " where tno = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,teacher.getTNO());
            ps.setString(2,teacher.getPassword());
            ps.setString(3,teacher.getName());
            ps.setString(4,teacher.getSex());
            ps.setString(5,teacher.getTel());
            ps.setString(6,teacher.getEmail());
            ps.setString(7,teacher.getCollegeId());
            ps.setString(8,teacher.getPost());
            ps.setString(9,teacher.getDepartment());
            ps.setString(10,teacher.getTNO());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }
    /**删除教师信息*/
    @Override
    public Integer teacherDelete(String tno, Connection conn){
        int result = 0;
        try {
            String sql = " delete from teacher where tno = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,tno);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }





    /**新增公告信息*/
    public Integer messageAdd(String msg_title,String msg_from,String msg_content,Connection conn){
        int result = 0;
        try {
            String sql = " insert into message( msg_title,msg_from,msg_content,msg_time) " +
                    " values (?,?,?,now()) ";
            ps = conn.prepareStatement(sql);
            ps.setString(1,msg_title);
            ps.setString(2,msg_from);
            ps.setString(3,msg_content);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return result;
    }
}
